package com.leyou.item.web;

import com.leyou.item.dto.*;
import com.leyou.item.service.GoodsService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author YYP
 * @date 2020/12/30
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询商品spu
     * @param page
     * @param rows
     * @param brandId
     * @param categoryId
     * @param id
     * @param saleable
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> findSpuByPage(
            @RequestParam(value = "page",defaultValue = "1") Long page,
            @RequestParam(value = "rows",defaultValue = "5") Long rows,
            @RequestParam(value = "brandId",required = false) Long brandId,
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "id",required = false) Long id,
            @RequestParam(value = "saleable",required = false) Boolean saleable
    ){

        return ResponseEntity.ok(goodsService.findSpuByPage(page,rows,brandId,categoryId,id,saleable));
    }

    /**
     * 新增商品（新增spu，spudetail，sku）
     * @param spuDTO
     * @return
     */
    @PostMapping("/spu")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO){
        goodsService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    /**
     * 更新商品
     * @param spuDTO
     * @return
     */
    //todo
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改商品的上下架
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam("id")Long id,@RequestParam("saleable") Boolean saleable){
        goodsService.updateSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     *
     根据id批量查询sku
     * @param skuIds
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> listSkuByIds(@RequestParam("ids")List<Long> skuIds){
        return ResponseEntity.ok(goodsService.listSkuByIds(skuIds));
    }

    /**
     *
     根据spuID查询sku的集合
     * @param spuId
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> listSkuBySpuId(@RequestParam("id") Long spuId){
        return ResponseEntity.ok(goodsService.listSkuBySpuId(spuId));
    }

    /**
     *
     根据id查询商品详情
     * @param spuId
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailBySpuId(@RequestParam("id") Long spuId){
        return ResponseEntity.ok(goodsService.querySpuDetailBySpuId(spuId));
    }

    /**
     *
     根据spuId查询spu的所有规格参数值
     * @param spuId
     * @return
     */
    //todo
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> listSpuParamsByQuery(
            @RequestParam("id") Long spuId,
            @RequestParam(value = "searching",required = false)Boolean searching){
        return ResponseEntity.ok(goodsService.listSpuParamsByQuery(spuId,searching));
    }

    /**
     * 根据id查询spu及sku、spuDetail等
     * @param spuId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> querySpuSkuDetailBySpuId(@PathVariable("id") Long spuId) {

        return ResponseEntity.ok(goodsService.querySpuSkuDetailBySpuId(spuId));
    }

    /**
     * 根据id查询商品
     * @param spuId
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuBySpuId(@PathVariable("id") Long spuId) {

        return ResponseEntity.ok(goodsService.querySpuBySpuId(spuId));
    }

    /**
     * 修改库存
     * @param params
     * @return
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> listJianStock(@RequestBody Map<String,Long> params){
        goodsService.listJianStock(params);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }



    /**
     * 修改库存
     * @param cartsMap
     * @return
     */
    @PutMapping("/sku/minus/stock")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long,Integer> cartsMap){

        this.goodsService.minusStock(cartsMap);
        return ResponseEntity.ok().build();
    }
}
